package com.hzx.ssm.controller;

import com.hzx.ssm.entity.ResBean;
import com.hzx.ssm.entity.User;
import com.hzx.ssm.service.UserService;
import com.hzx.ssm.util.CodeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @ResponseBody
    @PostMapping("registerUser")
    public ResBean registerUser(@RequestParam("username") String username,
                                @RequestParam("password") String password
                                ){

        return userService.registerUser(username, password);

    }


    @ApiOperation("用户登录")
    @ResponseBody
    @PostMapping("login")
    public ResBean login(@RequestParam("username") String username,
                                @RequestParam("password") String password) {
        return userService.login(username, password);

    }

    @ApiOperation("查找所有用户")
    @ResponseBody
    @GetMapping("getAllUser")
    public  ResBean getAllUser(){
        return userService.getAllUser();
    }

    @ApiOperation("根据用户名或Id查找用户")
    @ResponseBody
    @GetMapping("getUser")
    public  ResBean getUser(@RequestParam("username") String username,
                               @RequestParam("user_id") String user_id){
        return userService.getUser(username,user_id);
    }

    @ApiOperation("用户修改密码")
    @ResponseBody
    @PutMapping("updatePassword")
    public  ResBean updatePassword(@RequestParam("user_id") String user_id,
                                   @RequestParam("password") String password
                                   ){

        return userService.updatePassword(user_id,password);
    }

    @ApiOperation("用户修改个人信息")
    @ResponseBody
    @PutMapping("updateSelfMessage")
    public  ResBean updateSelfMessage(User user){
        return userService.updateSelfMessage(user);
    }

    @ApiOperation("修改用户头像")
    @ResponseBody
    @PostMapping("updateUserFace")
    public ResBean updateUserFace( @RequestParam(value = "face") MultipartFile face,
                                   HttpServletRequest req,
                                   @RequestParam("user_id")String user_id) throws IOException {
        String realPath = req.getServletContext().getRealPath("/imgFace/");
        File file = new File(realPath);
        if(!file.mkdir())
            System.out.println("创建文件成功");
        String originalFilename = face.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String facePath = realPath+user_id+suffix;
        face.transferTo(new File(facePath));

        return null;

    }







}
